package com.tiyende.credit.loop.configs

import com.tiyende.credit.loop.BuildConfig
import com.tiyende.credit.loop.sdk.analyse.AnalyseBuilder
import com.tiyende.credit.loop.sdk.analyse.AnalyseUtil
import com.tiyende.credit.loop.utils.TiyendeDateUtil
import com.tiyende.credit.loop.utils.shared.TiyendeSharedHelper
import com.tiyende.credit.loop.utils.shared.TiyendeSharedKey
import org.json.JSONObject
import java.util.Locale
import java.util.Random
import java.util.TimeZone

/**
 * ApiUrl
 * @Author wwdeng
 * @CreateTime: 2024/12/5 15:48
 * @Annotation：
 */
object TiyendeApiUrl {

    /**
     * https://www.zoeinfotech.online
     * http://159.138.165.72
     * 域名
     */
    const val TIYENDE_HOST = "http://159.138.165.72"

    /**
     * 地区转发
     * /v1/timezonePath
     */
    const val TIYENDE_TIMEZONE_URL = "/t0Nol5QvPB1WjkAkNnklmQ"

    /**
     * APP业务统一请求接口
     * /v1/commonApi
     */
    private val TIYENDE_COMMON_URLS = mutableListOf(
        "/Q8Ut3tBfpUdSXNg4afR52Q",
        "/gOGrHUhOVqREzO953yjXbA",
        "/E8J1hhc27ui0SqOg6RvImw",
        "/JWPHS4Pl02Bnagko4tA",
        "/H3Jrd6qogYrqalJf9KAjMQ",
        "/d1PmiEq23ysGMkCRwbHIrw"
    )

    /**
     * 图片上传
     * /v1/imageUpload
     */
    const val TIYENDE_UPLOAD_FILE_URL = "/c7QuIPWogHtZGhhtFEbQ"

    /**
     * 获取请求Url
     */
    fun getTiyendeHttpUrl(isUpload: Boolean = false, urlIndex: Int = -1): String{
        var url = getRandomUrl(urlIndex)
        if (isUpload){//上传
            url = TIYENDE_UPLOAD_FILE_URL
        }
        //地区转发
        val timeZone = TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_TIMEZONE)
        if (timeZone.isNotEmpty()){
            return "$TIYENDE_HOST/$timeZone$url"
        }
        return "$TIYENDE_HOST$url"
    }

    /**
     * 添加公共参数
     */
    fun JSONObject.addCommonParams(): JSONObject {
        apply {
            put("product", AnalyseBuilder.product)                                           //  产品 APP名称
            put("token", TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_TOKEN))                                               //  token
            put("apiVersion","v3")                                                      //  接口版本
            put("androidId", AnalyseUtil.getAndroidId())                                    //  安卓id
            put("advertId", AnalyseUtil.getAdId())                                          //  广告id
            put("aduid", AnalyseUtil.getAdId())                                             //  广告id
            put("aduidPath", AnalyseUtil.getAndroidId())                                    //  广告地址
            put("isLimitAdTrackingEnabled", AnalyseUtil.getAdEnabled().toString())          //  是否限制广告id，1是 0否
            put("userId", TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_USERID))                                             //  userId
            put("version", BuildConfig.VERSION_CODE.toString())                               //  app版本号
            put("timp", System.currentTimeMillis())                                           //  时间戳
            put("country", TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_TIMEZONE_COUNTRY))                                  //国家标识
            put("tzid", TimeZone.getDefault().id)                                             //当前时区id
            put("localTimestamp","${System.currentTimeMillis()}")                       //时间戳
            put("lang",Locale.getDefault().language)                                          //当前语言
//            put("path","")                                                                        //  传映射后的接口路径，不带前面的/号
        }
        return this.addCustomParams()
    }

    /**
     * 添加随机参数增量 4
     */
    private fun JSONObject.addCustomParams(): JSONObject {
        val loginMobile = TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_MOBILE)
        val launchTime = TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_APP_START_TIME)
        val loginTime = TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_LOGIN_TIME)
        val currentTime = TiyendeDateUtil.getFormatTime(System.currentTimeMillis(),"dd/MM/yyyy HH:mm:ss")

        put("loginMobile",loginMobile)
        val randomIndex = Random().nextInt(4)
        if (randomIndex == 1){
            apply {
                put("startTime",launchTime)
            }
            return this
        }
        if (randomIndex == 2){
            apply {
                put("startTime",launchTime)
                put("loginTime",loginTime)
            }
            return this
        }

        if (randomIndex == 3){
            apply {
                put("startTime",launchTime)
                put("loginTime",loginTime)
                put("currentTime",currentTime)
            }
        }
        return this
    }

    /**
     * 获取随机Url
     */
    fun getRandomUrl(index: Int = -1): String{
        var urlIndex = index
        if (urlIndex == -1 || urlIndex > TIYENDE_COMMON_URLS.size - 1){
            urlIndex = Random().nextInt(TIYENDE_COMMON_URLS.size)
        }
        return TIYENDE_COMMON_URLS[urlIndex]
    }
}